package com.cloud.wms.ui.main.adapter.provider;

import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import androidx.core.view.ViewCompat;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.wms.R;
import com.cloud.wms.ui.main.adapter.WaitTaskAdapter;
import com.cloud.wms.ui.main.entity.TaskMemberEntity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @Author:Chen
 * @Date: 2021-01-18 19:42
 * @Description:
 */
public class TaskMemberProvider extends BaseNodeProvider {

    @Override
    public int getItemViewType() {
        return WaitTaskAdapter.MEMBER;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_main_task_member;
    }

    @Override
    public void convert(@NotNull BaseViewHolder holder, BaseNode baseNode) {
        TaskMemberEntity node = (TaskMemberEntity) baseNode;
        holder.setText(R.id.tv_member_name, node.getMemberName())
                .setGone(R.id.ll_task_member, getAdapter().getData().size() == 1);
        setArrowSpin(holder, node, false);
    }

    @Override
    public void convert(@NotNull BaseViewHolder helper, @NotNull BaseNode data, @NotNull List<?> payloads) {
        for (Object payload : payloads) {
            if (payload instanceof Integer && (int) payload == WaitTaskAdapter.EXPAND_COLLAPSE_PAYLOAD) {
                // 增量刷新，使用动画变化箭头
                setArrowSpin(helper, data, true);
            }
        }
    }

    private void setArrowSpin(BaseViewHolder helper, BaseNode data, boolean isAnimate) {
        TaskMemberEntity entity = (TaskMemberEntity) data;

        ImageView imageView = helper.getView(R.id.iv_arrow);

        if (entity.isExpanded()) {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200)
                        .setInterpolator(new DecelerateInterpolator())
                        .rotation(0f)
                        .start();
            } else {
                imageView.setRotation(0f);
            }
        } else {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200)
                        .setInterpolator(new DecelerateInterpolator())
                        .rotation(90f)
                        .start();
            } else {
                imageView.setRotation(90f);
            }
        }
    }

    @Override
    public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
        // 这里使用payload进行增量刷新（避免整个item刷新导致的闪烁，不自然）
        getAdapter().expandOrCollapse(position, true, true, WaitTaskAdapter.EXPAND_COLLAPSE_PAYLOAD);
    }
}
